import React, {Component} from 'react';
//import pureRender from 'pure-render-decorator';
import {History, Link, browserHistory,hashHistory } from 'react-router';
import { connect } from 'react-redux';
import { logIn,logWechat } from '../Redux/Actions/authAction';
import { is, fromJS} from 'immutable';
import {AlertInfo} from './common/alert';

const history = process.env.NODE_ENV !== 'production' ? browserHistory : hashHistory;

class Login extends Component {
    constructor() {
        super();
        this.state = {
            username:'',
            password:'',
            btn:"登录",
            info:"",
            access_token:"",
            openid:""
        }
        this.handleLogin = ()=>{
            if(this.state.username == ''){
                this.setState({
                    info:"请输入您的手机号"
                })
                return false;
            }
            if(this.state.password == ''){
                this.setState({
                    info:"请输入密码"
                })
                return false;
            }
            const reg = /^1[3|4|5|7|8][0-9]{9}$/;
            if(!reg.test(this.state.username)){
                this.setState({
                    info:"请输入正确的手机号"
                })
                return false;
            }
            // this.setState({
            //     btn:"登录中..."
            // });

            let opt = {
                'username': this.state.username,
                'password': this.state.password,
                'openid' : this.state.openid,
                'access_token' : this.state.access_token
            };
            this.props.dispatch(logIn(opt));
        }
        this.textInput = (event)=>{
            const name = event.target.name
            this.setState({
                [name]:event.target.value
            })
        }
    }
    componentWillUpdate(nextProps, nextState) {
        const {authReducer} = nextProps;
        if(authReducer.isLoggedIn == false){
            return false;
        }else{
            history.push("/main/order");
        }
    }
    componentDidMount(nextProps, nextState) {
       if(this.props.location.search){
            fetch("http://api.jiebaoyun.net/auth/login-wechat/"+this.props.location.search,{
				method: "POST",
				headers: {
					"Content-Type": "application/json"
				},
				//body:JSON.stringify({code:codevalue[0]})
			})
			.then(response=>response.json())
			.then((data)=>{
                console.log(data)
                if(data.code == 0){
                    this.setState({
                        access_token:data.data.access_token,
                        openid:data.data.openid
                    })
                }else{
                    let opt = {
                        'token': data.data.token,
                        'userId': data.data.userId
                    };
                    this.props.dispatch(logWechat(opt));
                }
			}).catch((e)=>{
				console.log(e.message);
			});
        }
    }

    render() {
        //document.title="登录";
        //console.log("code:",this.props.location.search);
        return (
            <div className="onepage">
                <div className="login-box">
                    <div className="logo-img"></div>
                    <h3>捷报云</h3>
                    <input type="text" name="username" value={this.state.username} placeholder="请输入您的手机号" onChange={this.textInput}/>
                    <input type="password" name="password" value={this.state.password} placeholder="请输入您的密码" onChange={this.textInput}/>
                    <Link to="lost"></Link>
                    <button className="login-btn" onClick={this.handleLogin}>{this.state.btn}</button>
                    <Link to="signup"></Link>
                </div>
                <AlertInfo info={this.state.info} />
            </div>
        )
    }
}


export default connect((state) => {
    const { authReducer } = state;
    return {
        authReducer
    }
})(Login);


